package com.esdk.test.orm;

import com.esdk.esdk;
import com.esdk.sql.BatchInsert;
import com.esdk.sql.BatchReplace;
import com.esdk.sql.Delete;
import com.esdk.sql.datasource.FileConnectionPool;
import com.esdk.sql.orm.ABRowSet;
import com.esdk.sql.orm.ConnectionUtils;
import com.esdk.sql.orm.ParentRow;
import com.esdk.sql.orm.SelectFactory;
import com.esdk.utils.CacheMap;

import java.sql.Connection;
import java.sql.SQLException;

public class DF{
	private static final CacheMap<Long,SelectFactory> selectFactoryMap=new CacheMap<>(60*15,1000,(k,v,t)->v.close());
	
	static{
		ConnectionUtils.setDataSource(FileConnectionPool.getDataSource());
	}
	
	private static SelectFactory sf(){
		Long requestId=ConnectionUtils.getRequestId();
		SelectFactory sf=selectFactoryMap.get(requestId);
		if(sf==null){
			sf=new SelectFactory(ConnectionUtils.getConnection(requestId,true));
			selectFactoryMap.put(requestId,sf);
		}
		return sf;
	}
	
	private static Connection getConnection(){
		return ConnectionUtils.getConnection();
	}

	public static void closeCurrent(){
		Long requestId=ConnectionUtils.getRequestId();
		selectFactoryMap.remove(requestId);
		ConnectionUtils.closeConnection(requestId);
	}
			
	public static Delete delete(String tablename) throws SQLException {
		Delete delete=new Delete(tablename,getConnection());
		return delete;
	}
	
	public static <T extends ParentRow> void delete(ABRowSet<T> rs) throws Exception {
		if(!rs.isEmpty()) {
			Object md=rs.getFirstRow().getMetaData();
			String tablename=(String)esdk.reflect.getFieldValue(md,"TABLENAME");
			String pkfieldname=(String)esdk.reflect.getFieldValue(md,"PrimaryKey");
			Delete delete=new Delete(tablename,getConnection());
			delete.in(pkfieldname,rs.getNumbers(pkfieldname));
			delete.perform();
		}
	}
	
	public static BatchInsert batchInsert(String tablename) {
		return new BatchInsert(tablename,DF.getConnection());
	}
	
	public static BatchReplace batchReplace(String tablename) {
		return new BatchReplace(tablename,DF.getConnection());
	}
	
	public static boolean flush(ParentRow pr) throws SQLException {
		pr.setConnection(getConnection());
		return pr.flush();
	}

	/**检查停止*/
	public static CheckStopSelect checkStopSelect() throws Exception{
	  return sf().createSelect(CheckStopSelect.class);
	}

	public static CheckStopRow checkStopRow() throws Exception{
	  return sf().createRow(CheckStopRow.class);
	}
	
	public static CheckStopRow checkStopRow(Number pkid) throws Exception{
	  return sf().createRow(CheckStopRow.class,pkid,true);
	}

	
	/**订单返回扩展表*/
	public static OrderFeedbackExtentSelect orderFeedbackExtentSelect() throws Exception{
	  return sf().createSelect(OrderFeedbackExtentSelect.class);
	}

	public static OrderFeedbackExtentRow orderFeedbackExtentRow() throws Exception{
	  return sf().createRow(OrderFeedbackExtentRow.class);
	}
	
	public static OrderFeedbackExtentRow orderFeedbackExtentRow(Number pkid) throws Exception{
	  return sf().createRow(OrderFeedbackExtentRow.class,pkid,true);
	}

	/**测试表*/
	public static SampleSelect sampleSelect() throws Exception{
	  return sf().createSelect(SampleSelect.class);
	}

	public static SampleRow sampleRow() throws Exception{
	  return sf().createRow(SampleRow.class);
	}
	
	public static SampleRow sampleRow(Number pkid) throws Exception{
	  return sf().createRow(SampleRow.class,pkid,true);
	}

	/**系統中心表*/
	public static TestCenterSelect testCenterSelect() throws Exception{
	  return sf().createSelect(TestCenterSelect.class);
	}

	public static TestCenterRow testCenterRow() throws Exception{
	  return sf().createRow(TestCenterRow.class);
	}
	
	public static TestCenterRow testCenterRow(Number pkid) throws Exception{
	  return sf().createRow(TestCenterRow.class,pkid,true);
	}

	
	/**测试表*/
	public static TestDictSelect testDictSelect() throws Exception{
	  return sf().createSelect(TestDictSelect.class);
	}

	public static TestDictRow testDictRow() throws Exception{
	  return sf().createRow(TestDictRow.class);
	}
	
	public static TestDictRow testDictRow(Number pkid) throws Exception{
	  return sf().createRow(TestDictRow.class,pkid,true);
	}

	
	/**系统用户表*/
	public static TestUserSelect testUserSelect() throws Exception{
	  return sf().createSelect(TestUserSelect.class);
	}

	public static TestUserRow testUserRow() throws Exception{
	  return sf().createRow(TestUserRow.class);
	}
	
	public static TestUserRow testUserRow(Number pkid) throws Exception{
	  return sf().createRow(TestUserRow.class,pkid,true);
	}

	
}
